package com.hang.gymmanager.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.entity.InformEntity;
import com.hang.gymmanager.mapper.InformMapper;
import com.hang.gymmanager.req.AddInformReq;
import com.hang.gymmanager.req.UpdateInformReq;
import com.hang.gymmanager.service.InformService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author wyq
 * @version v1.0.0
 * @explain
 * @date 2024/4/1 15:25
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class InformServiceImpl extends ServiceImpl<InformMapper, InformEntity> implements InformService {

    @Override
    public Page<InformEntity> all(BasePageReq req) {
        Page page = new Page(req.getPageNum(),req.getPageSize());
        QueryWrapper<InformEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().orderByDesc(InformEntity::getCreateTime);
        Page page1 = this.getBaseMapper().selectPage(page, wrapper);
        return page1;
    }

    @Override
    public InformEntity detail(Integer informId) {
        return this.getBaseMapper().selectById(informId);
    }

    @Override
    public Integer add(AddInformReq req) {
        InformEntity entity = new InformEntity();
        BeanUtils.copyProperties(req,entity);
        entity.setCreateTime(LocalDateTime.now());
        this.saveOrUpdate(entity);
        return entity.getId();
    }

    @Override
    public void update(UpdateInformReq req) {
        InformEntity entity = new InformEntity();
        BeanUtils.copyProperties(req,entity);
        this.saveOrUpdate(entity);
    }

    @Override
    public void delete(Integer informId) {
        this.removeById(informId);
    }
}
